home *** CD-ROM | disk | FTP | other *** search
/ The Very Best of Atari Inside / The Very Best of Atari Inside 1.iso / mint / mntlb20 / lib / main.c < prev    next >
C/C++ Source or Header  |  1992-05-15  |  5KB  |  204 lines

  1. /* from Dale Schumacher's dLibs */
  2. /* heavily modified by ers and jrb */
  3.  
  4. #include <stddef.h>
  5. #include <stdlib.h>
  6. #include <stdio.h>
  7. #include <string.h>
  8. #include <osbind.h>
  9. #include <mintbind.h>
  10. #include <memory.h>
  11. #include <time.h>
  12. #include <fcntl.h>
  13. #include <unistd.h>
  14.  
  15. int    errno;
  16. int    __mint;        /* 0 for TOS, MiNT version number otherwise */
  17.  
  18. char    _rootdir;    /* user's preferred root directory */
  19.  
  20. clock_t _starttime;    /* 200 HZ tick when we started the program */
  21. clock_t _childtime;    /* time consumed so far by our children */
  22. FILE    _iob[_NFILE];    /* stream buffers initialized below */
  23.  
  24. /* functions registered by user for calling at exit */
  25. #ifdef __STDC__
  26. typedef void (*ExitFn)(void);
  27. #else
  28. typedef void (*ExitFn)();
  29. #endif
  30. ExitFn *_at_exit;
  31. int _num_at_exit;    /* number of functions registered - 1 */
  32.  
  33. /*
  34.  * get MiNT version number. Since this has to be done in supervisor mode,
  35.  * we might as well set the start-up time of the system here, too.
  36.  */
  37.  
  38. static void
  39. getMiNT()
  40. {
  41.     long *cookie;
  42.  
  43. /* get the system time in 200HZ ticks from the BIOS _hz_200 variable */
  44.     _starttime = *((unsigned long *) 0x4ba);
  45.     _childtime = 0;
  46.  
  47.     cookie = *((long **) 0x5a0);
  48.     if (!cookie)
  49.         __mint = 0;
  50.     else {
  51.         while (*cookie) {
  52.             if (*cookie == 0x4d694e54L) {    /* MiNT */
  53.                 __mint = cookie[1];
  54.                 return;
  55.             }
  56.             cookie += 2;
  57.         }
  58.     }
  59.     __mint = 0;
  60. }
  61.  
  62. /* supplied by the user */
  63. __EXTERN int main __PROTO((int, char **, char **));
  64.  
  65. /* in getbuf.c */
  66. __EXTERN void _getbuf __PROTO((FILE *));
  67.  
  68. void
  69. _main(_argc, _argv, _envp)
  70.     long _argc;
  71.     char **_argv, **_envp;
  72. {
  73.     register FILE *f;
  74.     register int i;
  75.     char *s, *t, *new;
  76.     extern int __default_mode__;    /* in binmode.c or defined by user */
  77.     extern short _app;    /* tells if we're an application or acc */
  78.  
  79.     _num_at_exit = 0;
  80.     errno = 0;
  81. /*
  82.  * check for MiNT
  83.  */
  84.     (void)Supexec(getMiNT);
  85.     if (__mint && _app)
  86.         (void)Pdomain(1);    /* set MiNT domain */
  87.  
  88. /*
  89.  * initialize UNIXMODE stuff. Note that this library supports only
  90.  * a few of the UNIXMODE variables, namely "b" (binary mode default)
  91.  * and "r<c>" (default root directory).
  92.  */
  93.     if ((s = getenv("UNIXMODE")) != 0) {
  94.         while (*s) {
  95.             if (*s == 'b')
  96.                 __default_mode__ = _IOBIN;
  97.             else if (*s == 'r' && s[1])
  98.                 _rootdir = *++s;
  99.             s++;
  100.         }
  101.     }
  102.  
  103.     if (_rootdir >= 'A' && _rootdir <= 'Z')
  104.         _rootdir = _rootdir - 'A' + 'a';
  105.  
  106. /*
  107.  * if we're running under MiNT, and the current drive is U:, then this
  108.  * must be our preferred drive
  109.  */
  110.     if (!_rootdir && __mint >= 9) {
  111.         if (Dgetdrv() == 'U'-'A')
  112.             _rootdir = 'u';
  113.     }
  114.  
  115. /* if stderr is not re-directed to a file, force 2 to console
  116.  * (UNLESS we've been run from a shell we trust, i.e. one that supports
  117.  *  the official ARGV scheme, in which case we leave stderr be).
  118.  */
  119.     if(!*_argv[0] && isatty(2))
  120.         (void)Fforce(2, -1);
  121.  
  122.     stdin->_flag = _IOREAD|_IOFBF|__default_mode__;
  123.     stdout->_flag = _IOWRT|_IOLBF|__default_mode__;
  124.     stderr->_flag = _IORW|_IONBF|__default_mode__;
  125.              /* some brain-dead people read from stderr */
  126.  
  127.     for(i = 0, f = _iob; i < 3; ++i, ++f) {    /* flag device streams */
  128.         if(isatty(f->_file = i))
  129.             f->_flag |= _IODEV;
  130.         else
  131.             if(f == stdout)    { /* stderr is NEVER buffered */
  132.             /* if stdout re-directed, make it full buffered */
  133.                 f->_flag &= ~(_IOLBF | _IONBF);
  134.                 f->_flag |=  _IOFBF;
  135.             }
  136.         _getbuf(f);    /* get a buffer */
  137.     }
  138.      for(i = 3; i < _NFILE; i++, f++) {
  139.       f->_flag = 0;        /* clear flags, if this is a dumped program */
  140.     }
  141.  
  142.     /* Fix up environment, if necessary. At present, the only variable
  143.      * affected is PATH; the "standard" path separators for PATH are
  144.      * ',' and ';' in the Atari world, but POSIX mandates ':'. This
  145.      * conflicts with the use of ':' as a drive separator, so we
  146.      * also convert names like A:\foo to /dev/A/foo
  147.      * NOTE: this conversion must be undone in spawn.c so that
  148.      * old fashioned programs will understand us!
  149.      */
  150.     for (i = 0; s = _envp[i]; i++) {    /* '=', NOT '==' */
  151.         if (s[0] == 'P' && s[1] == 'A' && s[2] == 'T' &&
  152.             s[3] == 'H' && s[4] == '=') {
  153.             new = (char *) alloca(4*strlen(s));
  154.             strncpy(new, s, 5);
  155.             t = new+5;
  156.             s += 5;
  157.             while (*s) {
  158.                 if (s[1] == ':') {    /* drive letter */
  159.                     *t++ = '/';
  160.                     *t++ = 'd'; *t++ = 'e'; *t++ = 'v';
  161.                     *t++ = '/'; *t++ = *s++; s++;
  162.                 } else if (*s == ';' || *s == ',') {
  163.                     *t++ = ':'; s++;
  164.                 } else if (*s == '\\') {
  165.                     *t++ = '/'; s++;
  166.                 } else {
  167.                     *t++ = *s++;
  168.                 }
  169.             }
  170.             *t++ = 0;
  171.             _envp[i] = strdup(new);
  172.             break;
  173.         }
  174.     }
  175.  
  176.     /* ANSI-Draft: A return from the initial call to the main 
  177.      * function is equivalent to calling the exit function with
  178.      * the value returned by the main function as its argument. If
  179.      * the main function executes a return that specifies no
  180.      * value, the termination status returned to the host
  181.      * environment is undefined. [section 2.1.2.2]
  182.      */
  183.     exit(main((int) _argc, _argv, _envp));
  184. }
  185.  
  186. __EXITING exit(status)
  187.     int status;
  188. {
  189.     register int i, f;
  190.  
  191.     for(i = _num_at_exit - 1; i >= 0; --i)
  192.         (*_at_exit[i])();
  193.  
  194.     for(i=0; i<_NFILE; ++i) {
  195.         f = _iob[i]._flag;
  196.         if(f & (_IORW | _IOREAD | _IOWRT))
  197.             if (_iob[i]._file <= 2) /* only flush std. streams */
  198.                 fflush(&_iob[i]);
  199.             else
  200.                 fclose(&_iob[i]);
  201.     }
  202.     _exit(status);
  203. }
  204.